
package com.dmcb.common.business.entities;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.List;

/**
 *
 * Created by a on 2015/3/11.
 */
public class JsonResult implements Serializable {
    Logger logger = LoggerFactory.getLogger(JsonResult.class);

    private static final long serialVersionUID = 7934763623184866074L;
    /**
     * 请求有错误提示
     */
    public static final int STATUS_ERROR = 0;
    /**
     * 请求成功
     */
    public static final int STATUS_SUCCESS = 1;
    /**
     * 请求警告
     */
    public static final int STATUS_WARN = 2;
    /**
     * 未知异常
     */
    public static final int STATUS_EXCEPTION = -1;

    private int status = STATUS_SUCCESS;
    private String msg;
    private Object data;
    private Object key;
    private String href;
    private int count;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public JsonResult() {
    }

    public JsonResult(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public boolean isSuccess() {
        return status == STATUS_SUCCESS;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        convertNullToEmpty(data);
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Object getKey() {
        return key;
    }

    public void setKey(Object key) {
        this.key = key;
    }

    public String getHref() {
        return href;
    }

    public void setHref(String href) {
        this.href = href;
    }

    @Override
    public String toString() {
        return "JsonResult{" +
                "status=" + status +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    /**
     * 把对象所有属性为 null 的转为 ""
     */
    public void convertNullToEmpty(Object obj) {
        if (obj == null || obj instanceof String || obj instanceof Integer) {
            return;
        }
        if (obj instanceof List) {
            List list = (List) obj;
            if (list.size() == 0) {
                return;
            }
            for (Object subObj : list) {
                convertNullToEmpty(subObj);
            }
            return;
        }
        Class classz = obj.getClass();
        // 获取所有该对象的属性值
        Field fields[] = classz.getDeclaredFields();
        // 遍历属性值，取得所有属性为 null 值的
        for (Field field : fields) {
            try {
                Type t = field.getGenericType();
                String type = t.toString();
                if (type.equals("class java.lang.String")) {
                    Method m = classz.getMethod("get"
                            + change(field.getName()));
                    Object subObj = m.invoke(obj);// 调用该字段的get方法
                    if (subObj == null || "null".equals(subObj)) {
                        Method mtd = classz.getMethod("set"
                                        + change(field.getName()),
                                new Class[]{String.class});// 取得所需类的方法对象
                        mtd.invoke(obj, new Object[]{""});// 执行相应赋值方法
                    }
                } else if (type.equals("java.util.List<?>")) {
                    Method m = classz.getMethod("get"
                            + change(field.getName()));
                    convertNullToEmpty(m.invoke(obj));
                }
            } catch (Exception e) {
                logger.error("convertNullToString error", e);
                return;
            }
        }
    }

    /**
     * @param src 源字符串
     * @return 字符串，将src的第一个字母转换为大写，src为空时返回null
     */
    public static String change(String src) {
        if (src != null) {
            StringBuffer sb = new StringBuffer(src);
            sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
            return sb.toString();
        } else {
            return null;
        }
    }

}
